﻿using FreeDream.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Reflection;

namespace FreeDream.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class GenerateController : ControllerBase
    {
        ///// <summary>
        ///// 数据连接对象 
        ///// </summary>
        //private SqlSugarScope _db;

        //public GenerateController(ISqlSugarClient sqlSugarClient)
        //{
        //    var _connectionString = AppSettings.App(new string[] { "ConnectionStrings", "SugarConnectString" });
        //    if (string.IsNullOrEmpty(_connectionString))
        //        throw new ArgumentNullException("数据库连接字符串为空");

        //    _db = sqlSugarClient as SqlSugarScope;
        //}

        ///// <summary>
        ///// 生成表
        ///// </summary>
        //[HttpGet, Route("GenerateTable")]
        //public void GenerateTable()
        //{
        //    // 创建数据库
        //    Console.WriteLine($"Create Database(The Db Id:{DateTime.Now})...");
        //    _db.DbMaintenance.CreateDatabase();
        //    Console.WriteLine($"Database created successfully!{DateTime.Now}");

        //    try
        //    {
        //        Console.WriteLine($"Create Tables...{DateTime.Now}");
        //        var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
        //        var referencedAssemblies = System.IO.Directory.GetFiles(path, "ZWC.Entity.dll").Select(Assembly.LoadFrom).ToArray();
        //        var modelTypes = referencedAssemblies
        //            .SelectMany(a => a.DefinedTypes)
        //            .Select(type => type.AsType())
        //            .Where(x => x.IsClass && x.Namespace != null && x.Namespace.Equals("ZWC.Entity")).ToList();

        //        modelTypes.ForEach(t =>
        //        {
        //            // 这里只支持添加表，不支持删除
        //            // 如果想要删除，数据库直接右键删除，或者联系SqlSugar作者；
        //            if (!_db.DbMaintenance.IsAnyTable(t.Name))
        //            {
        //                Console.WriteLine(t.Name);
        //                _db.CodeFirst.InitTables(t);
        //            }
        //        });

        //        Console.WriteLine($"Tables created successfully!  {DateTime.Now} ");
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.Message);
        //    }
        //}
    }
}
